
package org.sopt.dev.ImgKnight.monitor.chart;

import org.jfree.chart.JFreeChart;
import org.sopt.dev.ImgKnight.monitor.Monitor;


public class AgentChartSet {

	private Monitor monitor = null;
	
	private MemoryMeter memoryMeter = null;
	
	private FetchedUrlMeter fetchedUrlMeter = null;
	
	private FetchedResourceMeter fetchedResourceMeter = null;
	
	private SeenImageMeter seenImageMeter = null;
	
	private TraficMeter traficMeter = null;
	
	public AgentChartSet(Monitor monitor) {
		this.monitor = monitor;
		memoryMeter = new MemoryMeter(120000);
		fetchedUrlMeter = new FetchedUrlMeter(120000);
		fetchedResourceMeter = new FetchedResourceMeter(120000);
		seenImageMeter = new SeenImageMeter(120000);
		traficMeter = new TraficMeter(120000);
	}
	
	public JFreeChart createMemoryLineChart() {
		return memoryMeter.createChart();
	}
	
	public JFreeChart createFetchedUrlLineChart() {
		return fetchedUrlMeter.createLineChart();
	}
	
	public JFreeChart createFetchedUrlRCPieChart() {
		return fetchedUrlMeter.createBarChartForReservAndComplete();
	}
	
	public JFreeChart createFetchedUrlCDPieChart() {
		return fetchedUrlMeter.createPieChartForCompleteDetail();
	}
	
	public JFreeChart createFetchedResourceLineChart() {
		return fetchedResourceMeter.createLineChart();
	}
	
	public JFreeChart createDuplicatedImageLineChart() {
		return seenImageMeter.createDuplicatedImageLineChart();
	}
	
	public JFreeChart createFetchedResourceRCPieChart() {
		return fetchedResourceMeter.createBarChartForReservAndComplete();
	}
	
	public JFreeChart createFetchedResourceCDPieChart() {
		return fetchedResourceMeter.createPieChartForCompleteDetail();
	}
	
	public JFreeChart createSeenImageLineChart() {
		return seenImageMeter.createSeenImageLineChart();
	}
	
	public JFreeChart createTraficLineChart() {
		return traficMeter.createChart();
	}
	
	public void updateMemotyStatus(final double totalMemory, final double freeMemory) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				memoryMeter.addTotalObservation(totalMemory);
				memoryMeter.addUsedObservation(totalMemory-freeMemory);
			}
		});
	}
	
	public void updateFetchedUrlStatus(final double reservedUrlCount, final double takeUrlCount,
			final double discardUrlCount, final double missUrlCount) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				fetchedUrlMeter.addReservedMemPushObservation(reservedUrlCount);
				fetchedUrlMeter.addFetchObservation(takeUrlCount);
				fetchedUrlMeter.addDiscardObservation(discardUrlCount);
				fetchedUrlMeter.addMissObservation(missUrlCount);
				fetchedUrlMeter.addRemainObservation(reservedUrlCount-
						(takeUrlCount+discardUrlCount+missUrlCount));
				
				fetchedUrlMeter.setBarChartForRCDataReserved(reservedUrlCount);
				fetchedUrlMeter.setBarChartForRCDataCompleted((takeUrlCount+discardUrlCount+missUrlCount));
				
				fetchedUrlMeter.setPieChartForCompleteDetailData("Fetch", takeUrlCount);
				fetchedUrlMeter.setPieChartForCompleteDetailData("Discard", discardUrlCount);
				fetchedUrlMeter.setPieChartForCompleteDetailData("Miss", missUrlCount);
			}
		});
	}
	
	public void updateFetchedResourceStatus(final double reservedResourceCount, final double takeResourceCount,
			final double discardResouceCount, final double missResouceCount) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				fetchedResourceMeter.addReservedMemPushObservation(reservedResourceCount);
				fetchedResourceMeter.addFetchObservation(takeResourceCount);
				fetchedResourceMeter.addDiscardObservation(discardResouceCount);
				fetchedResourceMeter.addMissObservation(missResouceCount);
				fetchedResourceMeter.addRemainObservation(reservedResourceCount-
						(takeResourceCount+discardResouceCount+missResouceCount));
				
				fetchedResourceMeter.setBarChartForRCDataReserved(reservedResourceCount);
				fetchedResourceMeter.setBarChartForRCDataCompleted((takeResourceCount+discardResouceCount+missResouceCount));
				
				fetchedResourceMeter.setPieChartForCompleteDetailData("Fetch", takeResourceCount);
				fetchedResourceMeter.setPieChartForCompleteDetailData("Discard", discardResouceCount);
				fetchedResourceMeter.setPieChartForCompleteDetailData("Miss", missResouceCount);
			}
		});
	}
	
	public void updateDuplicatedImageStatus(final double duplicatedImageCount) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				seenImageMeter.addDuplicateObservation(duplicatedImageCount);
			}
		});
	}
	
	public void updateSeenImageStatus(final double seenImageCurrentCount, final double seenImageFlushedCount) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				seenImageMeter.addCurrentObservation(seenImageCurrentCount);
				seenImageMeter.addFlushedObservation(seenImageFlushedCount);
			}
		});
	}
	
	public void updateTraficStatus(final double trafic) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				traficMeter.addTraficObservation(trafic);
			}
		});
	}
}
